package com.xxl.job.core.biz.client;

import com.xxl.job.core.biz.XxlJobBiz;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.biz.model.XxlJobParam;
import com.xxl.job.core.util.XxlJobRemotingUtil;

public class XxlJobBizClient implements XxlJobBiz {

    private String addressUrl ;
    private String accessToken;
    private int timeout = 5;

    public XxlJobBizClient() {
    }

    public XxlJobBizClient(String addressUrl, String accessToken) {
        this.addressUrl = addressUrl;
        this.accessToken = accessToken;

        // valid
        if (!this.addressUrl.endsWith("/")) {
            this.addressUrl = this.addressUrl + "/";
        }
    }

    @Override
    public ReturnT<String> add(XxlJobParam jobInfo) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/extend/add", accessToken, timeout, jobInfo, String.class);
    }

    @Override
    public ReturnT<String> update(XxlJobParam jobInfo) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/extend/update", accessToken, timeout, jobInfo, String.class);
    }

    @Override
    public ReturnT<String> remove(int id) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/extend/remove", accessToken, timeout, id, String.class);
    }

    @Override
    public ReturnT<String> start(int id) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/extend/start", accessToken, timeout, id, String.class);
    }

    @Override
    public ReturnT<String> stop(int id) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/extend/stop", accessToken, timeout, id, String.class);
    }

    @Override
    public ReturnT<String> validate(int id) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/extend/validate", accessToken, timeout, id, String.class);
    }

    @Override
    public ReturnT<String> getGroupId(String appName) {
        return XxlJobRemotingUtil.postBody(addressUrl+"api/extend/getGroupId", accessToken, timeout, appName, String.class);
    }
}
